#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include <sys/wait.h>
int main()
{
    printf("我变成了一个进程：%d\n", getpid());

    pid_t id = fork();
    if(id == 0)
    {
        sleep(2);
        printf("下面的代码都是子进程在执行:\n");
        char *argv[] = { 
            (char*)"ls",
            (char*)"-a",
            (char*)"-l",
            NULL
        }; 
        execvp(argv[0],argv);
        //execv("/usr/bin/ls", argv);
        return 0;
    }
    
    // 父进程代码可以放在这里
    wait(NULL);  // 等待子进程结束
    printf("父进程结束\n");
    int status = 0;
    pid_t rid = waitpid(id, &status, 0);
    if(rid > 0)
    {
        printf("wait success, exit code: %d\n", WEXITSTATUS(status));
    }  
    return 0;
}

